<template>
  <div>
    <!--搜索-->
    <div class="panel-container">
      <searchuinew :searchFormArr="searchFormArr" @searchParma="searchParma" :isAccording="isAccording" ref="searchuinew"></searchuinew>
    </div>
    <!--数据信息-->
    <div class="panel-container no-margin">
      <div class="panel-line flex-end margin">
        <el-button size="mini" type="primary" @click="handleSendApply">批量发送</el-button>
      </div>
      <el-table :data="dataList" size="mini" :header-cell-style="{ background: '#F5F5F5', color: '#000000' }" ref="table" style="width: 100%" v-loading="dataListLoading" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="50"> </el-table-column>
        <el-table-column align="center" header-align="center" label="序号" type="index" width="50"></el-table-column>
        <el-table-column align="center" header-align="center" label="标题" prop="msg_title" width="200"></el-table-column>
        <el-table-column align="center" header-align="center" label="地区" prop="msg_zone"></el-table-column>
        <el-table-column align="center" header-align="center" label="crm链接地址" prop="crm_address"></el-table-column>
        <el-table-column align="center" header-align="center" label="外网链接" prop="online_url"></el-table-column>
        <el-table-column align="center" header-align="center" label="任务总数" prop="total"></el-table-column>
        <el-table-column align="center" header-align="center" label="发送状态" prop="status_show"></el-table-column>
        <el-table-column align="center" header-align="center" label="发送时间" prop="created_time"></el-table-column>
        <el-table-column align="center" fixed="right" header-align="center" label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="handleRecord(scope)">记录</el-button>
            <el-button size="mini" type="text" @click="handleDetail(scope)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        small
        background
        :current-page="pageIndex"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="totalPage"
        @current-change="currentChangeHandle"
        @size-change="sizeChangeHandle"
        layout="total, sizes, prev, pager, next, jumper"
        style="margin-top: 20px; text-align: right"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
import { apiPlanList, apiSendMany } from '@/api/assetsDisposal'
export default {
  name: 'index',
  components: {},
  data() {
    const stsList = [
      { id: 1, text: '未开始' },
      { id: 2, text: '进行中' },
      { id: 3, text: '已完成' },
    ]
    return {
      stsList,
      searchFormArr: [
        {
          fieldName: '标题：',
          type: 'input',
          field: 'title',
          placeholder: '请输入标题',
        },
        {
          fieldName: '区域：',
          type: 'input',
          field: 'zone',
          placeholder: '请输入区域',
        },
        {
          fieldName: '执行状态：',
          type: 'select',
          field: 'sts',
          option: stsList,
          placeholder: '请选择',
        },
      ],
      isAccording: true,
      searchParams: {
        title: '',
        zone: '',
        sts: '',
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      multipleSelection: [],
    }
  },
  mounted() {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      const info = Object.assign({ pageNumber: this.pageIndex, pageSize: this.pageSize }, this.searchParams)
      const formData = new FormData()
      for (let key in info) {
        formData.set(key, info[key])
      }
      apiPlanList(formData)
        .then((res) => {
          const { items, total } = res.result

          items.forEach((item) => {
            const stsItem = this.stsList.find((sts) => {
              return sts.id === item.is_start
            })
            if (stsItem) {
              item.status_show = stsItem.text
            }
          })
          this.dataList = items
          this.totalPage = total
          this.dataListLoading = false
        })
        .catch((e) => {
          this.dataList = []
          this.totalPage = 0
          this.dataListLoading = false
        })
    },
    // 列表查询
    searchParma(e) {
      console.log(e)
      this.searchFormArr.forEach((item) => {
        if (item.field === 'time') return
        if (e[item.field]) {
          this.searchParams[item.field] = e[item.field]
        } else {
          this.searchParams[item.field] = ''
        }
      })
      this.pageIndex = 1
      this.getDataList()
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 计划详情
    handleDetail(scope) {
      this.$emit('showPlanDetail', scope.row.id)
    },
    // 发送记录
    handleRecord(scope) {
      this.$emit('showSendRecord', scope.row.id)
    },
    // 表格复选框
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => {
        return item.id
      })
    },
    // 批量发送
    handleSendApply() {
      if (this.multipleSelection.length === 0) {
        this.$toast('请选择计划')
        return
      }
      this.$confirm(`确定执行选中计划？`).then((_) => {
        this.loading = true
        const sendData = { ids: this.multipleSelection }
        const formData = new FormData()
        for (let key in sendData) {
          formData.set(key, sendData[key])
        }
        apiSendMany(formData)
          .then((res) => {
            this.loading = false
            if (res.code !== 200) {
              return
            }
            this.$message({
              message: '操作成功！',
              type: 'success',
            })
            this.getDataList()
          })
          .catch(() => {
            this.loading = false
          })
      })
    },
  },
}
</script>

<style scoped lang="scss">
.no-margin {
  margin-top: -30px;
}
.margin {
  margin-bottom: 10px;
}
</style>
